package yue.shen.model

import org.itheima.kotlin.game.core.Painter
import yue.shen.business.AutoMoveAble
import yue.shen.business.DestroyAble
import yue.shen.enums.Direction
import yue.shen.other.Config
import yue.shen.view.View

/**
 *  子弹
 * Created by jiangzhuolei on 2017/11/8.
 */
class Bullet(override val currentDirection: Direction,create:(width:Int,height:Int)->Pair<Int,Int>) : AutoMoveAble , DestroyAble {



    override var x: Int = 0
    override var y: Int = 0
    override val width: Int
    override val height: Int
    override val speed: Int = 8

    init{
        val size = Painter.size("/img/enemymissile.gif")
        width = size[0]
        height = size[1]
        val pair = create.invoke(width,height)
        x = pair.first
        y = pair.second
    }

    override fun draw() {
        Painter.drawImage("/img/enemymissile.gif",x,y)
    }

    override fun autoMove() {
        //自己移动
        when(currentDirection){
            Direction.up->y -=speed
            Direction.down->y +=speed
            Direction.right->x +=speed
            Direction.left->x -=speed
        }
    }

    override fun isDestroyed(): Boolean {
        if(x < -width)return true
        if(x > Config.gameWidth)return true
        if(y < -height)return true
        if(y > Config.gameHeight)return true
        return false
    }
}